//
//  RMMPersonBandDingViewController.m
//  Romomo
//
//  Created by puman on 2016/11/23.
//  Copyright © 2016年 mazegong. All rights reserved.
//

#import "RMMPersonBandDingViewController.h"

@interface RMMPersonBandDingViewController ()<UITextFieldDelegate>

@end

@implementation RMMPersonBandDingViewController
{
    UITextField *_textPhone;
    UITextField *_yanText;
    UITextField *_bandDingCode;
    UIButton *_button;
    UIButton *_sendBut;
    NSInteger _number;
    NSString *_yanCode;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kCColor;
    self.navigationItem.title= _titleName;
    _number = 60;
    [self addViews];
    // Do any additional setup after loading the view.
}
- (void)addViews{
    _button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _button.backgroundColor = RGBA(238, 238, 238, 1);
    [_button setTitleColor:COLORFROMCODE(0xD7D7D7, 1) forState:(UIControlStateNormal)];
    _button.titleLabel.font = kBFont(16);
    _button.layer.masksToBounds = YES;
    _button.layer.cornerRadius = 22;
    _button.userInteractionEnabled = NO;
    BUTTON_S(_button, yanzheng);
    [_button setTitle:@"验证并绑定" forState:(UIControlStateNormal)];
    [self.view addSubview:_button];

    if ([_titleName isEqualToString:@"绑定手机号"]) {
        _button.frame = CGRectMake(kScreenWideth / 2 - 130,188, 260, 44);
        for (int i =0; i < 2; i++) {
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 16 +70 * i, kScreenWideth, 54)];
            view.backgroundColor = kWhiteColor;
            [self.view addSubview:view];
            
            if (i == 0) {
                UILabel *label =  [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, view.height)];
                label.text = @"中国+86";
                label.textAlignment = NSTextAlignmentCenter;
                label.textColor = COLORFROMCODE(0x2C2C2C, 1);
                label.font = kBFont(16);
                [view addSubview:label];
                
                UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(label.right, 0, 1, view.height)];
                lineLabel.backgroundColor = kCColor;
                [view addSubview:lineLabel];
                
                _textPhone = [[UITextField alloc] initWithFrame:CGRectMake(lineLabel.right + kRow, 0, kScreenWideth- lineLabel.right - kRow, view.height)];
                _textPhone.delegate = self;
                _textPhone.textColor = COLORFROMCODE(0x2C2C2C, 1);
                _textPhone.font = kFont(16);
                _textPhone.placeholder = @"输入手机号码";
                _textPhone.clearButtonMode = UITextFieldViewModeWhileEditing;
                _textPhone.keyboardType = UIKeyboardTypeNumberPad;
                [view addSubview:_textPhone];
                [_textPhone addTarget:self action:@selector(numberTextFieldChang:) forControlEvents:(UIControlEventEditingChanged)];
                
                _sendBut = [UIButton buttonWithType:(UIButtonTypeCustom)];
                _sendBut.frame = CGRectMake(0,0,kScreenWideth,40);
                _sendBut.backgroundColor = RGBA(238, 238, 238, 1);
                [_sendBut setTitleColor:COLORFROMCODE(0xD7D7D7, 1) forState:(UIControlStateNormal)];
                _sendBut.titleLabel.font = kBFont(16);
                [_sendBut setTitle:@"发送验证码" forState:(UIControlStateNormal)];
                [self.view addSubview:_button];
                BUTTON_S(_sendBut, sendMessage);
                _sendBut.userInteractionEnabled = NO;
                _textPhone.inputAccessoryView = _sendBut;
                
            } else {
                _yanText = [[UITextField alloc] initWithFrame:CGRectMake(15, 0, kScreenWideth - 30, view.height)];
                _yanText.delegate = self;
                _yanText.textColor = COLORFROMCODE(0x2C2C2C, 1);
                _yanText.font = kFont(16);
                _yanText.placeholder = @"输入验证码";
                _yanText.clearButtonMode = UITextFieldViewModeWhileEditing;
                _yanText.keyboardType = UIKeyboardTypeNumberPad;
                [_yanText addTarget:self action:@selector(numberTextFieldChang:) forControlEvents:(UIControlEventEditingChanged)];
                UIButton *button = [RMMFileManager addInputViewForTextField];
                _yanText.inputAccessoryView = button;
                BUTTON_S(button, doneBut);
                [view addSubview:_yanText];
                
            }
        }

    } else {
        _button.frame = CGRectMake(kScreenWideth / 2 - 130,128, 260, 44);
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 16 , kScreenWideth, 54)];
        view.backgroundColor = kWhiteColor;
        [self.view addSubview:view];
        _bandDingCode = [[UITextField alloc] initWithFrame:CGRectMake(15, 0, kScreenWideth - 30, view.height)];
        _bandDingCode.delegate = self;
        _bandDingCode.textColor = COLORFROMCODE(0x2C2C2C, 1);
        _bandDingCode.font = kFont(16);
        _bandDingCode.text = _codes;
        UIButton *button = [RMMFileManager addInputViewForTextField];
        _bandDingCode.inputAccessoryView = button;
        BUTTON_S(button, doneBut);
        [view addSubview:_yanText];
        if ([_titleName isEqualToString:@"绑定微信"]) {
            _bandDingCode.placeholder = @"输入您微信号";
        } else {
            _bandDingCode.placeholder = @"输入您QQ号";
        }
        _bandDingCode.clearButtonMode = UITextFieldViewModeWhileEditing;
        _bandDingCode.keyboardType = UIKeyboardTypeNumberPad;
        [_bandDingCode addTarget:self action:@selector(textFieldChang:) forControlEvents:(UIControlEventEditingChanged)];
        [view addSubview:_bandDingCode];

    }
    
}
// 发送验证码
- (void)sendMessage{
    [_textPhone resignFirstResponder];
    if (![RMMFileManager isPhoneNumberWith:_textPhone.text]) {
        [RMMFileManager MBProgressWith:@"手机号码输入错误!" onView:self.view];
        return;
    }
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"mobile"] = _textPhone.text;
    NSString *url = SendMessageUrl;
    [OSRequestManager requestWithUrlString:url parDic:parDic method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            _yanCode = dataDic[@"data"];
            [self countDown];
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
        
    } error:^(NSError *errors) {
        
    } view:self.view];
}
// 倒计时
- (void)countDown{
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer:) userInfo:@"倒计时" repeats:YES];
    [self butOff:_sendBut];
    [timer fire];
    
}
- (void)timer:(NSTimer *)timer{
    if (_number == 0) {
        [timer invalidate];
        [_sendBut setTitle:@"收取验证码" forState:(UIControlStateNormal)];;
        _number = 60;
    }
    [_sendBut setTitle:[NSString stringWithFormat:@"%ld秒后重新发送发",_number] forState:(UIControlStateNormal)];
    _number--;
}

- (void)butOpen:(UIButton *)button{
    button.backgroundColor = kBasicColor;
    [button setTitleColor:kWhiteColor forState:(UIControlStateNormal)];
    button.userInteractionEnabled = YES;
}
- (void)butOff:(UIButton *)button{
    button.backgroundColor = RGBA(238, 238, 238, 1);
    [button setTitleColor:COLORFROMCODE(0xD7D7D7, 1) forState:(UIControlStateNormal)];
    button.userInteractionEnabled = NO;
}
- (void)numberTextFieldChang:(UITextField *)textF{
    if (textF == _textPhone) {
        if (textF.text.length >= 11) {
            textF.text = [textF.text substringToIndex:11];
            [self butOpen:_sendBut];
        } else {
            [self butOff:_sendBut];
        }
    }
    if (_textPhone.text.length != 0 && _yanText.text.length != 0) {
        [self butOpen:_button];
    } else {
        [self butOff:_button];
    }
}
- (void)textFieldChang:(UITextField *)textF{
    if (textF.text.length != 0) {
        [self butOpen:_button];
    } else {
        [self butOff:_button];
    }
    
}
- (void)yanzheng{
    [self.view endEditing:YES];
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"org_no"] = Org_no;
    parDic[@"login_type"] = [RMMFileManager shareHandle].userDic[@"LOGIN_TYPE"];
    NSString *string = nil;
    NSString *custorlogin_id = nil;
    NSDictionary *dic = [RMMFileManager shareHandle].userDic;
    if ([dic[@"LOGIN_TYPE"] isEqualToNumber:@0]) {
        custorlogin_id = dic[@"CUST_ID"];
    } else {
        custorlogin_id = dic[@"LOGIN_ID"];
    }
    parDic[@"custorlogin_id"] = custorlogin_id;
    if ([_titleName isEqualToString:@"绑定手机号"]) {
        parDic[@"phone"] = _textPhone.text;
        string = _textPhone.text;
        if ([_yanText.text isEqualToString:_yanCode]) {
            [RMMFileManager MBProgressWith:@"验证码输入错误!" onView:self.view];
            return;
        } 
    } else if ([_titleName isEqualToString:@"绑定QQ"]) {
        parDic[@"qq"] = _bandDingCode.text;
        string = _bandDingCode.text;

    } else {
        parDic[@"wx"] = _bandDingCode.text;
        string = _bandDingCode.text;
    }
    NSString *url = BangDingUrl;
    [OSRequestManager requestWithUrlString:url parDic:parDic method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            [MMProgressHUD show];
            [MMProgressHUD dismissWithSuccess:@"绑定成功!" title:nil afterDelay:1];
            NSString *cust_id = dataDic[@"data"];
            NSDictionary *dic = @{@"cust_id":cust_id};
            NS_POST(@"PERSONREFRESH", nil, dic);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                _blocks(string, cust_id);
                [self.navigationController popViewControllerAnimated:YES];
            });
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
    } error:^(NSError *errors) {
        
    } view:self.view];

}
- (void)doneBut{
    [self.view endEditing:YES];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
